import { AxiosRequestConfig } from "axios";
import { BaseResponse, PageData } from "src/util";
import { PermissionRecord } from "src/views/role/RoleType";
import { request } from "../util";
// export interface PermissionRecord {
// 	id: number;
// 	name: string;
// 	url: string;
// 	parentId: string;
// 	type: string;
// 	icon: string;
// 	method: string;
// 	createdTime: string;
// 	updatedTime: string;
// }
export const getPermissions = async ({ page, pageSize } = { page: 1, pageSize: 10 }) => {
	const { data } = await request.get<BaseResponse<PageData<PermissionRecord[]>>>("/admin/permission", { params: { page, pageSize } });
	return data;
};
export const getRolePermissions = async (
	{ page, pageSize, roleId }: { page: number; pageSize: number; roleId: string | null } = { page: 1, pageSize: 10, roleId: null }
) => {
	const { data } = await request.get<BaseResponse<PageData<PermissionRecord[]>>>("/admin/permission/role", {
		params: { page, pageSize, roleId },
	});
	return data;
};
export const getUnAccreditedPermissions = async (
	{ page, pageSize, roleId }: { page: number; pageSize: number; roleId: string | null } = { page: 1, pageSize: 10, roleId: null }
) => {
	const { data } = await request.get<BaseResponse<PageData<PermissionRecord[]>>>("/admin/permission/unAccredit", {
		params: { page, pageSize, roleId },
	});
	return data;
};
export const cancelPermissionAuth = async (id: string, roleId: string) => {
	const { data } = await request.delete<BaseResponse>("/admin/permission/cancelAuth", { data: { id, roleId } });
	return data;
};
interface CreatePermissionReq {
	url: string;
	method?: AxiosRequestConfig["method"];
	name: string;
	type?: 1 | 2 | 3;
	parentId?: null | number;
}
export const createPermission = async (reqData: CreatePermissionReq) => {
	const { data } = await request.post<BaseResponse>("/admin/permission/create", reqData);
	return data;
};
export const updatePermission = async (reqData: CreatePermissionReq & { id: string }) => {
	const { data } = await request.patch<BaseResponse>("/admin/source/permission", reqData);
	return data;
};
interface CreateAndAuthPermissionReq extends CreatePermissionReq {
	role_id: string;
}
export const createAndAuthPermission = async (reqData: CreateAndAuthPermissionReq) => {
	const { data } = await request.post<BaseResponse>("/admin/permission/createAndAuth", reqData);
	return data;
};

export const accreditPermission = async (reqData: { role_id: string; permission_id: string }) => {
	const { data } = await request.post<BaseResponse>("/admin/permission/auth", reqData);
	return data;
};
